home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2FG16.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  7.0 KB  |  252 lines

  1. #include <fastgraf.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <alloc.h>
  7.  
  8. /*
  9. ╔═════════════════════════════════════════════════════════════════╗
  10. ║             █▓▒░ WordUp Graphics Toolkit V4.0 ░▒▓█              ║
  11. ║ Conversion Utility   Copyright 1994 WordUp Software Productions ║
  12. ╟─────────────────────────────────────────────────────────────────╢
  13. ║ Program:      spr2fg16.c                                        ║
  14. ║ Description:  Utility for converting a WGT sprite file to       ║
  15. ║               a Fastgraph format. (16 colors)                   ║
  16. ║                                                                 ║
  17. ║ Written by:   Chris Egerter                                     ║
  18. ╚═════════════════════════════════════════════════════════════════╝
  19. */
  20.  
  21. /*
  22. ╔═════════════════════════════════════════════════════════════════╗
  23. ║  WGT Sprite File to Fastgraph conversion program                ║
  24. ║                                                                 ║
  25. ║  The file format is not one already used in Fastgraph, but      ║
  26. ║  an altered version of the WGT sprite format.                   ║
  27. ║                                                                 ║
  28. ║  Note that this version works only in 16 color modes.           ║
  29. ╚═════════════════════════════════════════════════════════════════╝
  30. */
  31.  
  32.  
  33. unsigned char *sprites[2001];
  34. void convertsprites (char *infile, char *outfile);
  35. void loadsprites (char *infile, void far *loadspr[]);
  36. void testsprites ();
  37. void freesprites (void far *freespr[]);
  38. int oldmode;
  39. int width[2001],height[2001];
  40.  
  41. int main(void)
  42. {
  43.  
  44.    oldmode=fg_getmode();
  45.    fg_setmode(16);  /* This mode can be any graphics mode */
  46.  
  47.    convertsprites("sprtconv.spr","out.spr");
  48.    loadsprites("out.spr",sprites);
  49.    testsprites();
  50.    freesprites(sprites);
  51.  
  52.  
  53.    /* clean up */
  54.    fg_setmode(oldmode);
  55.    return 0;
  56. }
  57.  
  58. void convertsprites(char *infile,char *outfile)
  59. {
  60. FILE *in;   /* 256 color sprite file */
  61. FILE *out;  /* converted sprite file */
  62. unsigned char palette[768];         /* 256 * 3 (RGB values) */
  63. int maxcolor;
  64. int maxsprite;
  65. long size;
  66.  
  67. void far *temp;
  68. int a,b,i,spritemade;
  69. char buf[14];
  70. unsigned char c;
  71. int x,y;
  72. int startingsprite;
  73.  
  74.     /* Open the files */
  75.     if ((in = fopen (infile, "rb")) == NULL)
  76.        {
  77.        fg_setmode(oldmode);
  78.        printf("Could not open 256 color sprite file");
  79.        exit(1);
  80.        }
  81.     if ((out = fopen (outfile, "wb")) == NULL)
  82.        {
  83.        fg_setmode(oldmode);
  84.        printf("Could not open converted sprite file");
  85.        exit(1);
  86.        }
  87.  
  88.  fread(&a, 1, 2, in);
  89.      /* Get the version number, and change the startingsprite accordingly.
  90.      If version <= 3, maxsprite contains the maximum number of sprites
  91.      that can be stored in a file.  If version > 4, maxsprite contains
  92.      the number of the highest sprite in the file. (empty sprites at
  93.      the end are not kept in the file. */
  94.  if (a <= 3)
  95.    startingsprite = 1;
  96.  else startingsprite = 0;    /* Version 4 starts at sprite 0 */
  97.  
  98.     fread (buf, 1, 13, in); /* sprite header */
  99.     if (0 == strnicmp (" Sprite File ", buf, 13)) /* see if it is a sprite file */
  100.     {
  101.     fread(palette,1,768,in); /* Read in 256 color palette */
  102.     maxcolor=256;
  103.         fwrite(&maxcolor, 1, 2, out); 
  104.         /* Write the number of colors stored in file */
  105.  
  106.     for (i = 0; i < maxcolor; i++) /* read in the palette */
  107.         {
  108.        fputc(palette[i*3],out);    /* Write out Red */
  109.        fputc(palette[i*3+1],out);  /* Green */
  110.        fputc(palette[i*3+2],out);  /* And Blue color values */
  111.  
  112.         }
  113.         
  114.        fread(&maxsprite, 1, 2, in); /* maximum sprites in this file */
  115.        fwrite(&maxsprite, 1, 2, out); 
  116.        for (i = startingsprite; i <= maxsprite; i++) /* load them in */
  117.          {
  118.           fread(&spritemade, 1, 2, in);    /* flag to see if sprite exists */
  119.           fwrite(&spritemade, 1, 2, out); 
  120.           if (spritemade == 1)
  121.             {
  122.         fg_setcolor(0);
  123.         fg_rect(0,fg_getmaxx(),0,fg_getmaxy());  /* maximum sprite size */
  124.                 fread(&a, 1, 2, in); /* get width and height */
  125.                 fread(&b, 1, 2, in);
  126.                 fwrite(&a, 1, 2, out); /* put width and height */
  127.                 fwrite(&b, 1, 2, out); 
  128.  
  129.         /* Read in the image data. Each byte represents a color
  130.         from 0-255. Obviously converting sprites that use more colors
  131.         than the current mode allows will not work. Draw sprites using
  132.         only the first colors (eg 0-16), up to maxcolors of the mode you're using. */
  133.         for (y=0; y<b; y++)
  134.           for (x=0; x<a; x++)
  135.              {
  136.              c=fgetc(in);
  137.              fg_setcolor(c);
  138.              fg_point(x,y);
  139.              }
  140.  
  141.         size = fg_imagesiz(a, b); /* get byte size of image */
  142.         if ((temp = farmalloc(size)) == NULL)
  143.             {
  144.               fg_setmode(oldmode);
  145.               printf("Error: not enough heap space in convertsprites.\n");
  146.               exit(1);
  147.            }
  148.         fg_move(0,b);
  149.         fg_getimage(temp,a/2, b); /* Get the image in new mode */
  150.         fwrite(temp,size,1,out); /* Write the data in getimage format */
  151.         farfree(temp);
  152.         }
  153.         }
  154.     }
  155.     fclose (in);
  156.     fclose (out);
  157. }
  158.  
  159. void loadsprites(char *infile,void far *loadspr[])
  160. {
  161. FILE *in;   /* converted color sprite file */
  162. int maxsprite;
  163. long size;
  164.  
  165. int a,b,i,spritemade;
  166. int maxcolor;
  167. struct {
  168.       unsigned char red,green,blue;
  169.       }
  170.       pal[256];
  171.  
  172.     /* Open the files */
  173.     if ((in = fopen (infile, "rb")) == NULL)
  174.        {
  175.        fg_setmode(oldmode);
  176.        printf("Could not load converted color sprite file");
  177.        exit(1);
  178.        }
  179.  
  180.         fread(&maxcolor, 1, 2, in);
  181.  
  182.     for (i = 0; i < maxcolor; i++) /* read in the palette */
  183.        {
  184.        pal[i].red=fgetc(in);
  185.        pal[i].green=fgetc(in);
  186.        pal[i].blue=fgetc(in);
  187.        }
  188.  
  189.         fread(&maxsprite, 1, 2, in); /* maximum sprites in this file */
  190.  
  191.     for (i = 1; i <= maxsprite; i++) /* load them in */
  192.     {
  193.         spritemade = getw (in); /* flag to see if sprite exists */
  194.         if (spritemade == 1)
  195.         {
  196.                 fread(&a, 1, 2, in);  /* get width and height */
  197.                 fread(&b, 1, 2, in);
  198.         width[i]=a/2;
  199.         height[i]=b;
  200.  
  201.         size = fg_imagesiz(a, b); /* get byte size of image */
  202.         if ((loadspr[i] = farmalloc(size)) == NULL)
  203.             {
  204.               fg_setmode(oldmode);
  205.               printf("Error: not enough heap space in loadsprites().\n");
  206.               freesprites(loadspr);
  207.               exit(1);
  208.            }
  209.         fread(loadspr[i],size,1,in);
  210.         } 
  211.         else loadspr[i]=NULL;
  212.     
  213.     }
  214.     fclose(in);
  215. }
  216.  
  217.  
  218.  
  219.  
  220. void testsprites(void)
  221. /* Loops through 10 sprites, displaying them on the screen
  222.    using the putimage method. Press a key to go to the next sprite. */
  223. {
  224. int i,j;
  225.  
  226. for (i=0; i<10; i++) 
  227.   {
  228.   fg_erase();
  229.  
  230.    if (sprites[i] !=NULL)
  231.      {
  232.      for (j=1; j<20; j++)
  233.        {
  234.        fg_move(rand() % fg_getmaxx(),rand() % fg_getmaxy());
  235.        fg_drwimage(sprites[i],width[i],height[i]);
  236.        }
  237.        /* Put the converted image on the screen */
  238.      getch();
  239.      }
  240.   }
  241. }
  242.  
  243. void freesprites(void far *freespr[])
  244. {
  245. int i;
  246.  
  247. for (i=0; i < 2001; i++)
  248.   {
  249.   if (freespr[i] !=NULL)
  250.     farfree(freespr[i]);
  251.   }
  252. }